class_name FlickeringTexutreRect
extends TextureRect

# 闪烁一次视为先可见再变为不可见，故每次可见性变化的时间是闪烁的总时间除以总次数的一半


## 闪烁的时长（秒）
@export var duration_flicker: float
## 闪烁的次数
@export var total_flicker: int


var is_flickering: bool = false
# 每次闪烁的时间的一半
var duration_half_flicher_per_sec: float

var time_flicker: float
var time_visible: float


func _ready():
	visible = false
	duration_half_flicher_per_sec = duration_flicker / total_flicker / 2.0


func _process(delta: float):
	if is_flickering:
		process_flickering(delta)


func start_flickering():
	if is_flickering:
		return
	time_flicker = duration_flicker
	time_visible = duration_half_flicher_per_sec
	is_flickering = true
	SoundPlayer.play(SoundManager.Sound.GO)



func process_flickering(delta: float):
	if time_visible < 0.0:
		if time_flicker < 0.0:
			flicker_completed()
			return
		visible = !visible
		time_visible = duration_half_flicher_per_sec
		return

	time_visible -= delta
	time_flicker -= delta


func flicker_completed():
	is_flickering = false
	visible = false
